package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    @RequestMapping("findAll")
    public Result findAll(){
        Set<Role> list=roleService.findAll();
        return new Result(true,"查询成功",list);
    }
    //条件枫叶查询角色
    @PostMapping("/listByPage/{page}/{limit}")
    public Result listByPage(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit , @RequestBody Map<String,String> query1){
        String query = query1.get("query");
        PageResult rolePage= roleService.listByPage(page,limit,query);
        return new Result(true,"查询角色列表成功",rolePage);
    }
//根据角色id查询当前角色的权限
@RequestMapping("/findPermissionByRoleId/{id}")
    public Result findPermissionByRoleId(@PathVariable("id") Integer id ){
          Set<Permission> list= roleService.findPermissionByRoleId(id);
        return  new Result(true,"查询角色对应的权限成功",list);
}

//修改角色权限
@RequestMapping("/editPermissionByRoleId/{roleId}")
    public  Result savePermissionByRoleId(@RequestParam("checkPermission") Integer[] checkPermission,@PathVariable("roleId") Integer roleId ){
    System.out.println("roleId"+roleId);
        roleService.savePermissionByRoleId(roleId,checkPermission);
   return new Result(true,"修改角色权限成功");
}
//添加角色
    @RequestMapping("addRole")
    public  Result addRole(@RequestBody Role role){
        roleService.addRole(role);
        return new Result(true,"添加角色成功");
    }
    //修改角色
    @RequestMapping("editRole")
    public  Result editRole(@RequestBody Role role){
        roleService.editRole(role);
        return new Result(true,"修改角色成功");
    }
    //根据id查询角色
    @RequestMapping("/findRoleById/{id}")
    public Result findRoleById(@PathVariable("id") Integer id ){
        Role role =roleService.findRoleById(id);
        return new Result(true,"查询角色成功",role);
    }
     //保存菜单by角色id
     @RequestMapping("/editMenuByRoleId/{id}")
     public Result  editMenuByRoleId(@PathVariable("id") Integer id,@RequestParam Integer[] menuIds ){
         roleService.editMenuByRoleId(id,menuIds);
         return new Result(true,"修改菜单成功");
     }
     //查询当前角色拥有的菜单id
     @RequestMapping("/findMenusByRoleId/{id}")
     public Result  findMenusByRoleId(@PathVariable("id") Integer id){
         Set<Integer> list= roleService.findMenusByRoleId(id);
         return new Result(true,"查询角色拥有菜单id成功",list);
     }
     //删除角色by角色id
    @RequestMapping("/deleteByRoleId/{id}")
    public  Result deleteByRoleId(@PathVariable Integer id){
        roleService.deleteByRoleId(id);
        return new Result(true,"删除角色成功");
    }
    //删除角色by角色ids
    @RequestMapping("/deleteRoleByRoleIds")
    public  Result deleteRoleByRoleIds(@RequestParam Integer[] ids){
        roleService.deleteRoleByRoleIds(ids);
        return new Result(true,"删除角色成功");
    }
}
